Shader "Custom/Ring"
{
    Properties
    {
        _MainTex("Image", 2D) = "white" {}
        _MinRadius("Min Radius", Float) = 0.4 
        _MaxRadius("Max Radius", Float) = 0.05
        _Angle("Angle", Range(0, 360)) = 360.0
    }
    SubShader
    {
        Pass
        {
            Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }

            Blend One DstColor
            BlendOp Sub

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct a2v
            {
                float4 pos: POSITION;
                float2 uv: TEXCOORD0;
                float4 color : COLOR;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv: TEXCOORD0;
                float4 color : COLOR;
            };

            sampler2D _MainTex;
            uniform float _MinRadius;
            uniform float _MaxRadius;
            uniform float _Angle;

            v2f vert(a2v i)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(i.pos);
                o.uv = i.uv;
                o.color = i.color;
                return o;
            }

            fixed4 frag(v2f i) : COLOR
            {
                const float2 mid = float2(0.5, 0.5);
                clip(_Angle - 0.01f);
                float d = distance(i.uv, mid);
                clip(d - _MinRadius);
                clip(_MaxRadius - d);
                
                float2 dir = (i.uv - mid) / d;

                // theta = if (dir.x < 0) { 360 - deg(acos(dir.y)) } else { deg(acos(dir.y)) };
                float theta = (1 - ceil(dir.x)) * 360.0 + sign(dir.x) * (acos(dir.y) * 57.29578);

                clip(_Angle - theta);
                
                return i.color;
            }

            ENDCG
        }
    }
    FallBack "UI/Default"
}
